package com.example.administrator.myjdlc.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.myjdlc.AppApplication;
import com.example.administrator.myjdlc.R;
import com.example.administrator.myjdlc.utils.DensityUtil;


public class ProgressUi extends Dialog
{
    private static final int imageWidth = 40;
    private static final int padding = 15;
    int layoutRes;
    private ImageView ivImage;
    private TextView tvTxt;
    private String showTxt;
    Animation an;

    public void setShowTxt(String showTxt)
    {
        this.showTxt = showTxt;
    }

    public void setShowTxt(int id)
    {
        this.showTxt = AppApplication.getContext().getString(id);
    }

    public ProgressUi()
    {
        super(AppApplication.getTopActivity(), R.style.loading_dialog);
        this.layoutRes = R.layout.dialog;
        this.showTxt = AppApplication.getContext().getString(R.string.loading);
        setCancelable(false);
    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(this.layoutRes);
        setCancelable(false);
        this.ivImage = ((ImageView)findViewById(R.id.iv_image));
        this.tvTxt = ((TextView)findViewById(R.id.tv_txt));
        setAnimation();
        if (this.showTxt.equals(""))
        {
            this.tvTxt.setVisibility(View.GONE);
        }
        else
        {
            this.tvTxt.setText(this.showTxt);

            Paint paint = new Paint();
            float textWidth = paint.measureText(this.showTxt);
            int width = DensityUtil.dip2px(AppApplication.getContext(), 70.0F);
            if (textWidth > 40.0F) {
                if (DensityUtil.dip2px(AppApplication.getContext(), textWidth) > UiManager.screenWidth * 3 / 5) {
                    width = UiManager.screenWidth * 3 / 5;
                } else {
                    width = DensityUtil.dip2px(AppApplication.getContext(), textWidth + 30.0F);
                }
            }
            ViewGroup.LayoutParams params = this.tvTxt.getLayoutParams();
            params.width = width;
        }
    }

    @SuppressLint({"NewApi"})
    public void setAnimation()
    {
        if (this.an == null)
        {
            this.an = new RotateAnimation(0.0F, 360.0F, 1, 0.5F, 1, 0.5F);
            this.an.setInterpolator(new LinearInterpolator());
            this.an.setRepeatCount(-1);
            this.an.setFillAfter(true);
            this.an.setDuration(1000L);
        }
        this.ivImage.startAnimation(this.an);
    }

    public void show()
    {
        if (isShowing()) {
            dismiss();
        }
        super.show();
        setAnimation();
        if (this.showTxt.equals(""))
        {
            this.tvTxt.setVisibility(View.GONE);
        }
        else
        {
            this.tvTxt.setText(this.showTxt);

            Paint paint = new Paint();
            float textWidth = paint.measureText(this.showTxt);
            int width = DensityUtil.dip2px(AppApplication.getContext(), 70.0F);
            if (textWidth > 40.0F) {
                if (DensityUtil.dip2px(AppApplication.getContext(), textWidth) > UiManager.screenWidth * 3 / 5) {
                    width = UiManager.screenWidth * 3 / 5;
                } else {
                    width = DensityUtil.dip2px(AppApplication.getContext(), textWidth + 30.0F);
                }
            }
            ViewGroup.LayoutParams params = this.tvTxt.getLayoutParams();
            params.width = width;
        }
    }

    public void dismiss()
    {
        super.dismiss();
        if (this.ivImage != null) {
            this.ivImage.clearAnimation();
        }
    }
}
